﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{

    /// <summary>
    ///  lay 50 video bai hat Viet noi bat trong tuan tren mp3 zing
    /// </summary>
    public class ZingVideoHot
    {
        private List<Mp3Video> _ListHotVideo;

        public List<Mp3Video> ListHotVideo
        {
            get { return _ListHotVideo; }
            private set { _ListHotVideo = value; }
        }

        private List<Mp3Video> _ListNewVideo;

        public List<Mp3Video> ListNewVideo
        {
            get { return _ListNewVideo; }
            private set { _ListNewVideo = value; }
        }

        private string _LinkHotVideo;
        private string _LinkNewVideo;

        private bool _IsSetLink;
        private bool _IsSetHotVideo;
        private bool _IsSetNewVideo;

        public ZingVideoHot()
        {
            _ListHotVideo = new List<Mp3Video>();
            _ListNewVideo = new List<Mp3Video>();
            _LinkHotVideo = "";
            _LinkNewVideo = "";
            _IsSetHotVideo = false;
            _IsSetLink = false;
            _IsSetNewVideo = false;
            
        }

        public async Task SetAllInformation()
        {
            try
            {
                await GetHotVideoLocalLink(@"http://m.mp3.zing.vn/the-loai-video.html");
                await SetHotVideo();
                await SetNewVideo();
            }
            catch(Exception ex)
            {
                throw (ex);
            }
        }

        private async Task SetHotVideo()
        {
            if (_IsSetHotVideo)
                return;
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(_LinkHotVideo));
                string pattern = @"(?<=autoload-src=).*?(?=>)";
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(source);
                string tempLink = mc[0].Value;
                int i = 0;
                while (i < tempLink.Length)
                {
                    if (tempLink[i] == 34 || tempLink[i] == 92)
                    {
                        tempLink = tempLink.Remove(i--, 1);
                    }
                    i++;
                }
                string DirectLink = @"http://m.mp3.zing.vn" + tempLink;

                /// lay 10 video dau tien

                string link = DirectLink + "&offset=0#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                List<Mp3Video> Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListHotVideo.Add(video);
                }

                /// 10 video tiep theo
                link = DirectLink + "&offset=10#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListHotVideo.Add(video);
                }

                /// 10 video tiep theo
                link = DirectLink + "&offset=20#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListHotVideo.Add(video);
                }

                /// 10 video tiep theo
                link = DirectLink + "&offset=30#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListHotVideo.Add(video);
                }

                /// 10 video tiep theo
                link = DirectLink + "&offset=40#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListHotVideo.Add(video);
                }
                _IsSetHotVideo = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private async Task SetNewVideo()
        {
            if (_IsSetNewVideo)
                return;
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(_LinkNewVideo));
                string pattern = @"(?<=autoload-src=).*?(?=>)";
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(source);
                string tempLink = mc[0].Value;
                int i = 0;
                while (i < tempLink.Length)
                {
                    if (tempLink[i] == 34 || tempLink[i] == 92)
                    {
                        tempLink = tempLink.Remove(i--, 1);
                    }
                    i++;
                }
                string DirectLink = @"http://m.mp3.zing.vn" + tempLink;

                /// lay 10 video dau tien

                string link = DirectLink + "&offset=0#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                List<Mp3Video> Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListNewVideo.Add(video);
                }

                /// 10 video tiep theo
                link = DirectLink + "&offset=10#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListNewVideo.Add(video);
                }

                /// 10 video tiep theo
                link = DirectLink + "&offset=20#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListNewVideo.Add(video);
                }

                /// 10 video tiep theo
                link = DirectLink + "&offset=30#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListNewVideo.Add(video);
                }

                /// 10 video tiep theo
                link = DirectLink + "&offset=40#itemList.html";
                source = await Mp3Zing.DownloadString(new Uri(link));
                Get10Video = GetVideo(source);
                foreach (Mp3Video video in Get10Video)
                {
                    _ListNewVideo.Add(video);
                }
                _IsSetNewVideo = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private async Task GetHotVideoLocalLink(string url)
        {
            if (_IsSetLink)
                return;
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(url));
                string pattern = @"(?=the-loai-video/Viet-Nam).*?(html)";
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(source);
                string tempLink = @"http://m.mp3.zing.vn/" + mc[0].Value;

                source = await Mp3Zing.DownloadString(new Uri(tempLink));
                pattern = @"(?=the-loai-video/Viet-Nam).*?(total_play)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);

                _LinkHotVideo = @"http://m.mp3.zing.vn/" + mc[0].Value;

                pattern = @"(?=the-loai-video/Viet-Nam).*?(release_date)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);

                _LinkNewVideo = @"http://m.mp3.zing.vn/" + mc[0].Value;
                _IsSetLink = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private List<Mp3Video> GetVideo(string source)
        {
            List<Mp3Video> tempList = new List<Mp3Video>();

            List<string> LocalLinks = GetListLocalinkVideo(source);
            List<string> SingerName = GetListSingerName(source);
            List<string> ListName = GetListName(source);
            List<string> ListImgLink = GetListImgLink(source);


            int i = 0;
            foreach (string link in LocalLinks)
            {
               tempList.Add(new Mp3Video(ListName[i], SingerName[i], ListImgLink[i], link));
                i++;
            }

            return tempList;
        }

        private List<string> GetListLocalinkVideo(string source)
        {
            List<string> tempList = new List<string>();
            string pattern = @"(?<=href=).*?(html)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);

            foreach (Match temp in mc)
            {
                string s = temp.Value;
                int i = 0;
                while (i < s.Length)
                {
                    if (s[i] == 34 || s[i] == 92)
                    {
                        s = s.Remove(i--, 1);
                    }
                    i++;
                }
                s = @"http://m.mp3.zing.vn" + s;
                tempList.Add(s);
            }

            return tempList;
        }

        private List<string> GetListSingerName(string source)
        {
            List<string> listSingerName = new List<string>();
            string pattern = @"(?<=<h4>).*?(?=<)";
            Regex regex3 = new Regex(pattern);
            MatchCollection mc3 = regex3.Matches(source);
            foreach (Match m1 in mc3)
            {
                string temp2 = m1.Value;
                temp2 = Mp3Zing.ConvertStringAsciiToUtf8(temp2);
                listSingerName.Add(temp2);

            }
            return listSingerName;
        }

        private List<string> GetListName(string source)
        {
            //source = Mp3Zing.ConvertStringAsciiToUtf8(source);
            List<string> listSongAlbumName = new List<string>();
            string pattern = @"(?<=<h3>).*?(?=<)";
            Regex regex2 = new Regex(pattern);
            MatchCollection mc2 = regex2.Matches(source);
            foreach (Match m1 in mc2)
            {
                string temp2 = m1.Value;
                int i = 0;
                while (i < temp2.Length-1)
                {
                    if (temp2[i] == temp2[i + 1] && temp2[i] == 92)
                        temp2 = temp2.Remove(i--, 1);
                    i++;
                }
                temp2 = Mp3Zing.ConvertStringAsciiToUtf8(temp2);
                listSongAlbumName.Add(temp2);

            }
            return listSongAlbumName;
        }

        private List<string> GetListImgLink(string source)
        {
            List<string> listLink = new List<string>();
            string pattern = @"(?<=src=).*?(jpg|png)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            int j;
            foreach (Match m1 in mc)
            {
                string s = m1.Value;
                int i = 0;
                while (i < s.Length)
                {
                    if (s[i] == 34 || s[i] == 92)
                    {
                        s = s.Remove(i--, 1);
                    }
                    i++;
                }
                listLink.Add(s);
            }
            return listLink;
        }

    }
}
